import request from '@/utils/request'

// 角色查询参数
export interface RoleQueryParams {
  current?: number
  size?: number
  roleName?: string
  roleCode?: string
  status?: number
}

// 创建角色请求
export interface CreateRoleRequest {
  roleName: string
  roleCode: string
  description?: string
  roleType: number
  dataScope: number
  sort?: number
  status?: number
  remark?: string
  menuIds?: number[]
}

// 更新角色请求
export interface UpdateRoleRequest {
  roleName: string
  roleCode: string
  description?: string
  roleType: number
  dataScope: number
  sort?: number
  status?: number
  remark?: string
  menuIds?: number[]
}

// 角色API
export const roleApi = {
  // 分页查询角色
  getRolePage: (params: RoleQueryParams) => {
    return request.get('/roles/page', { params })
  },

  // 获取所有启用的角色
  getEnabledRoles: () => {
    return request.get('/roles/enabled')
  },

  // 获取角色详情
  getRoleById: (id: number) => {
    return request.get(`/roles/${id}`)
  },

  // 新增角色
  createRole: (data: CreateRoleRequest) => {
    return request.post('/roles', data)
  },

  // 更新角色
  updateRole: (id: number, data: UpdateRoleRequest) => {
    return request.put(`/roles/${id}`, data)
  },

  // 删除角色
  deleteRole: (id: number) => {
    return request.delete(`/roles/${id}`)
  },

  // 批量删除角色
  batchDeleteRole: (ids: number[]) => {
    return request.delete('/roles/batch', { data: { ids } })
  },

  // 启用/禁用角色
  changeStatus: (id: number, status: number) => {
    return request.put(`/roles/${id}/status`, { status })
  },

  // 分配菜单权限
  assignMenus: (id: number, menuIds: number[]) => {
    return request.put(`/roles/${id}/menus`, { menuIds })
  },

  // 获取角色菜单权限
  getRoleMenus: (id: number) => {
    return request.get(`/roles/${id}/menus`)
  },

  // 检查角色编码是否存在
  checkRoleCode: (roleCode: string, excludeId?: number) => {
    return request.get('/roles/check-code', { 
      params: { roleCode, excludeId } 
    })
  }
} 